Imports System.DirectoryServices

Public Class Delegates

    Private _dsDelegatorsToDelegates As DataSet

#Region "Singleton Interface"

    Private Shared _instance As Delegates

    Public Shared Function GetInstance() As Delegates
        Try
            If _instance Is Nothing Then
                _instance = New Delegates
            End If
        Finally
        End Try

        Return _instance

    End Function

    Private Sub New()

    End Sub

#End Region

    Public Function GetDelegatorsToDelegates() As DataSet

        If Me._dsDelegatorsToDelegates Is Nothing Then
            BuildDelegatorsToDelegates()
        End If

        Return _dsDelegatorsToDelegates

    End Function

    Private Sub BuildDelegatorsToDelegates()

        _dsDelegatorsToDelegates = New DataSet

        Dim myAD As ADHelper = ADHelper.GetInstance

        Dim groupFilter As String
        If My.Settings.UserManagementTargetGroupCN.Length > 0 Then
            groupFilter = "CN=" & My.Settings.UserManagementTargetGroupCN & "," & My.Settings.LDAPOUDN
        Else
            groupFilter = ""
        End If

        Dim filter As String = ""

        Dim properties As String = "cn,sn,givenname,SAMAccountName,mail,initials,publicDelegates,publicDelegatesBL,lastLogonTimestamp,modifyTimeStamp"

        Dim dt As DataTable
        dt = myAD.getAccounts(myAD.LDAPRoot, "user", groupFilter, properties, filter)

        Dim dv As DataView = New DataView(dt)

        dv.RowFilter = "publicDelegates <> ''"

        Dim dtDelegators As DataTable = dv.ToTable
        dtDelegators.TableName = "Delegators"

        _dsDelegatorsToDelegates.Tables.Add(dtDelegators)

        Dim dtDelegates As DataTable = BuildDelegatesTable(dtDelegators)

        dtDelegates.TableName = "Delegates"

        _dsDelegatorsToDelegates.Tables.Add(dtDelegates)

    End Sub

    Private Function BuildDelegatesTable(ByVal Delegators As DataTable) As DataTable

        Dim myAD As ADHelper = ADHelper.GetInstance

        Dim dtDelegates As DataTable = New DataTable
        With dtDelegates.Columns
            .Add("Delegator")
            .Add("cn")
            .Add("sn")
            .Add("givenname")
            .Add("SAMAccountName")
            .Add("mail")
        End With

        'for each delagator, find their full list of delegates
        Dim dr As DataRow
        For Each dr In Delegators.Rows

            Dim de As DirectoryEntry = New DirectoryEntry(myAD.LDAPRoot)

            de.AuthenticationType = AuthenticationTypes.Secure

            Dim ds As DirectorySearcher = New DirectorySearcher

            With ds
                .SearchRoot = de
                .Filter = "(cn=" & dr("cn") & ")"
                .PageSize = 50
            End With

            Dim userSearch As SearchResult = ds.FindOne()

            Dim userDE As DirectoryEntry = New DirectoryEntry(userSearch.Path)

            Dim s As String
            For Each s In userDE.Properties("publicdelegates")
                Dim drNew As DataRow = dtDelegates.NewRow
                With drNew
                    .Item("Delegator") = dr("cn")
                    .Item("cn") = s
                End With
                dtDelegates.Rows.Add(drNew)
            Next

        Next

        Return dtDelegates

    End Function

End Class
